/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.impl;


import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.audit.AuditResult;
import com.ceair.flight.refund.dto.audit.BaseAuditReq;
import com.ceair.flight.refund.dto.audit.IAuditResult;
import com.ceair.flight.refund.dto.external.BasePageQueryReq;
import com.ceair.flight.refund.location.dt.enums.ApplyReason;
import com.ceair.flight.refund.location.dt.enums.AuditPhase;
import com.ceair.flight.refund.location.dt.enums.AuditStatus;
import com.ceair.flight.refund.location.dt.enums.RefundApplyUserType;
import com.ceair.flight.refund.location.dt.enums.RefundStatus;
import com.ceair.flight.refund.service.audit.ISecondAuditService;
import com.ceair.flight.refund.service.uatp.IGenRefNoService;
import com.ceair.flight.refund.util.DBUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Colin_You
 */
@Service("autoSecondAuditServiceImpl")
public class AutoSecondAuditServiceImpl extends AbstractScheduleServiceImpl {

    private static final Logger LOG = LoggerFactory.getLogger(AutoSecondAuditServiceImpl.class);
    @Value("${auto.second.audit.switch}")
    private boolean autoSwitch;
    @Autowired
    private ISecondAuditService secondAuditService;
    @Autowired
    private IGenRefNoService genRefNoService;
    private static final Set<ApplyReason> AUTO_REASONS = new HashSet<>(Arrays.asList(ApplyReason.AUTO_SECOND_AUDIT_REASONS));

    @Autowired
    @Qualifier("dbUtil")
    private DBUtil dbUtil;

    @Override
    void doScheduleTask(RefundApplyDto refundApply) {
        if (autoSwitch) {
            LOG.debug("Refund apply {} starting auto second audit", refundApply.getId());
            BaseAuditReq auditReq = createAuditReq(refundApply);
            doPass(auditReq);
            LOG.debug("Refund apply {} finished auto second audit", refundApply.getId());
        } else {
            LOG.warn("The second audit switcher is off");
        }
    }

    /**
     * 构建所有个人原因,风控失败,用户拒付的自动通过一审的, 且正处于等待二审退票申请单查询条件
     *
     * @return
     */
    @Override
    BasePageQueryReq buildQueryReq() {
        BasePageQueryReq pageQueryReq = new BasePageQueryReq();
        pageQueryReq.addRefundStatus(RefundStatus.WAIT_SECOND_AUDIT);
        pageQueryReq.setAuditPhase(AuditPhase.FIRST_ADUIT);
        pageQueryReq.setAuditStatus(AuditStatus.PASS);
        pageQueryReq.setReasons(AUTO_REASONS);
        pageQueryReq.setPageSize(20);
        return pageQueryReq;
    }

    private BaseAuditReq createAuditReq(RefundApplyDto refundApply) {
        BaseAuditReq auditReq = new BaseAuditReq();
        auditReq.setApplyId(refundApply.getId());
        auditReq.setAuditName(RefundApplyUserType.SYSTEM.name());
        auditReq.setRemark("系统自动二审通过");
        return auditReq;
    }

    private IAuditResult doPass(BaseAuditReq auditReq) {
        AuditResult result;
        try {
            RefundApplyDto refundApply = secondAuditService.doAuditPass(auditReq, true);

            LOG.info("Refund apply {} pass auto second audit", refundApply.getId());
            result = new AuditResult(false, null);
            try {
                //生成退票单号和更改退票申请单状态
                genRefNoService.doGenRefNo(refundApply);
            } catch (Exception e) {
                //ignore
            }
        } catch (Exception e) {
            result = new AuditResult(true, e.getMessage());
            LOG.error("Second audit pass failed for refund apply {}", auditReq.getApplyId(), e);
        }
        return result;
    }

}
